package com.ly.video.api.user.controller;


import com.ly.video.api.user.entity.InComeEntity;
import com.ly.video.api.user.entity.UserEntity;
import com.ly.video.api.user.entity.WithdrawalEntity;
import com.ly.video.api.user.impl.UserServiceImpl;
import com.ly.video.common.util.JsonResult;
import com.ly.video.common.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
@CrossOrigin(origins = "*")
public class UserController {

    @Autowired
    private UserServiceImpl userService;


    /**
     * 用户登录
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public JsonResult<UserEntity> login(String openid){
        JsonResult<UserEntity> result = new JsonResult<>();
        // 查询登录
        UserEntity user = userService.login(openid);
        result.setEnum(ResultEnum.CR_200);
        result.setData(user);
        return result;
    }


    /**
     * 绑定father UID
     */
    @RequestMapping(value = "bind_father_uid", method = RequestMethod.POST)
    public JsonResult<UserEntity> bindFatherUid(String uid, String father) {
        JsonResult<UserEntity> result = new JsonResult<>();
        UserEntity user = userService.bindFatherUid(father, uid);
        result.setEnum(ResultEnum.CR_200);
        result.setData(user);
        return result;
    }

    /**
     * 用户观看激励视频广告
     */
    @RequestMapping(value = "adv/watch_video", method = RequestMethod.POST)
    public JsonResult<UserEntity> userWatchVideoAdv(String uid){
        JsonResult<UserEntity> result = new JsonResult<>();
        UserEntity user = userService.userWatchVideoAdv(uid);
        result.setEnum(ResultEnum.CR_200);
        result.setData(user);
        return result;
    }

    /**
     * 用户观看其他广告增加权重
     */
    @RequestMapping(value = "adv/watch_other", method = RequestMethod.POST)
    public JsonResult<Void> userWatchOtherAdv(String uid) {
        JsonResult<Void> result = new JsonResult<>();
        userService.userWatchOtherAdv(uid);
        result.setEnum(ResultEnum.CR_200);
        return result;
    }

    /**
     * 获取用户下级信息
     */
    @RequestMapping(value = "sons", method = RequestMethod.POST)
    public JsonResult<List<UserEntity>> getSonsInfo(Integer page, Integer size,String uid){
        JsonResult<List<UserEntity>> result = new JsonResult<>();
        List<UserEntity> userSons = userService.findUserSons((page - 1) * size, size, uid);
        result.setEnum(ResultEnum.CR_200);
        result.setData(userSons);
        return result;
    }

    /**
     * 获取收入信息,分页倒序
     */
    @RequestMapping(value = "getInComes", method = RequestMethod.GET)
    public JsonResult<List<InComeEntity>> getInComeList(Integer page, Integer size, String uid){
        JsonResult<List<InComeEntity>> result = new JsonResult<>();
        List<InComeEntity> inComeList = userService.findUserInComeInfo((page - 1) * size, size, uid);
        result.setEnum(ResultEnum.CR_200);
        result.setData(inComeList);
        return result;
    }

    /**
     * 用户提现
     */
    @RequestMapping(value = "withdrawal", method = RequestMethod.POST)
    public JsonResult<UserEntity> withdrawal(String amount, String uid){
        JsonResult<UserEntity> result = new JsonResult<>();
        UserEntity user = userService.withdrawal(amount, uid);
        if (user == null) {
            result.setEnum(ResultEnum.CR_301);
        } else {
            result.setEnum(ResultEnum.CR_200);
            result.setData(user);
        }
        return result;
    }

    /**
     * 获取用户提现信息
     */
    @RequestMapping(value = "getWithdrawalInfo", method = RequestMethod.POST)
    public JsonResult<List<WithdrawalEntity>> getUserWithdrawalInfo(Integer page, Integer size, String uid){
        JsonResult<List<WithdrawalEntity>> result = new JsonResult<>();
        List<WithdrawalEntity> withdrawalInfo = userService.getUserWithdrawalInfo((page - 1) * size, size, uid);
        result.setEnum(ResultEnum.CR_200);
        result.setData(withdrawalInfo);
        return result;
    }

}

